//
//  OrderForm_Vm.swift
//  Pods
//
//  Created by eme on 2017/3/31.
//
//

import RxSwift
import UtilCore
import SwiftyJSON
import ModelProtocol
import NetWorkCore

/*
 owner:cy
 update:2016年12月19日17:15:05
 info: 进货单
 VC:   OrderForm_Vc
 */
class OrderForm_Vm :Base_Vm {
    
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    /*
     接收返回的数据
     */
    /// 返回首页数据
    let productItem = PublishSubject<OrderFormModel>()
    
    /// 删除购物车
    let delSubject = PublishSubject<String>()
    let delSuccess = PublishSubject<Void>()
    ///觅客商城 修改进货单的数量和选中状态
    /*
     osCarts:	json{}
     //json字符串key
     cartid:
     required
     string"8808"
     购物车id
     cartnumber:
     required
     string"2"
     购物车数量
     ischeck:
     required
     string"1"
     是否选中 是否选择0否1是
     */
    let updateCartSubject = PublishSubject<String>()
    init(refreshTriger: Observable<Void>) {
        super.init()
        refreshTriger
            .map{ OrderFormApi.cartlist }
            .emeRequestApiForObj(OrderFormModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.productItem.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        ///觅客商城 删除购物车商品
        delSubject
            .map{ OrderFormApi.del(cartids: $0) }
            .emeRequestApiForObj(OrderFormModel.self,activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.productItem.onNext(data)
                    self.delSuccess.onNext()
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        ///觅客商城 修改进货单的数量和选中状态
        updateCartSubject
            .map{ OrderFormApi.updatenumbercheck(osCarts: $0 as NSString) }
            .emeRequestApiForRegJson()
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    print(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            })
            .disposed(by: disposeBag)
        
    }
    
}
